<template>
  <div>
    <!-- echarts图表容器 -->
    <div ref="main" style="width: 600px;height:400px;"></div>
  </div>
</template>
<script>
import echarts from 'echarts'
import axios from 'axios'
export default {
  name: 'EchartsDemo',
  data () {
    return {
      myChart: null
    }
  },
  mounted () {
    var echarts = require('echarts')

    // 基于准备好的dom，初始化echarts实例
    this.myChart = echarts.init(this.$refs.main)
    this.myChart.setOption({
      title: {
        text: '异步数据加载示例'
      },
      tooltip: {},
      legend: {
        data: ['销量']
      },
      xAxis: {
        data: []
      },
      yAxis: {},
      series: [{
        name: '销量',
        type: 'bar',
        data: []
      }]
    })
    this.loadData()
  },
  methods: {
    async loadData () {
      const { data } = await axios({
        method: 'get',
        url: 'http://127.0.0.1:8080/data.json' // public 里的东西可以直接发请求 这里直接请求的是 localhost:8080/data.json
      })
      // 填入数据
      this.myChart.setOption({
        xAxis: {
          data: data.categories
        },
        series: [{
          name: '销量',
          data: data.data
        }]
      })
    }
  }
}
</script>
<style scoped>
</style>
